local planetC = require("planeta")
local collectorC = require("collector")

local sheets = {} 
sheets.atmosphere = 1
sheets.kiwi = 2	
sheets.planet0 = 3	
sheets.planet1 = 4	
sheets.planet2 = 5	
sheets.planet3 = 6	
sheets.planet4 = 7	
sheets.planet5 = 8	
sheets.star = 9
sheets.tree1 = 10	

M = {}

local function split(pString, pPattern)
    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
    end
    return Table
end

local function getSheetName(filename)
    local tPath = split(filename, "\\")
    return string.sub(tPath[#tPath],0,string.len(tPath[#tPath])-4)
end

local newLevel = function(levelXml,imageSheet)
    local backgroundGroup = display.newImageGroup( imageSheet )
    local atmGroup = display.newGroup()
    local planetGroup = display.newImageGroup( imageSheet )
    
    local xml = require("xml").newParser()
    local level = xml:loadFile(levelXml)
    local layer = level.child[1]
    for i=1,#layer.child do
        if(layer.child[i].properties["Name"] == "PLANETAS")then
            local planetas = layer.child[i].child[1]
            for j=1,#planetas.child do
                local planeta = planetas.child[j]
                local x = planeta.child[1].child[1].value
                local y = planeta.child[1].child[2].value
                local scale = planeta.child[4].child[1].value
                local filename = planeta.child[8].value
                local sheetName = getSheetName(filename)
                local radio = planeta.child[10].child[1].value
                local planet1 = planetC.newPlanet(imageSheet,sheets[sheetName],x,y,radio * scale,200);
                planetGroup:insert(planet1)
                backgroundGroup:insert( planet1.atmosferaCircle )
                
            end
        end
        if(layer.child[i].properties["Name"] == "COLLECTORS")then
            local collectors = layer.child[i].child[1]
            for k=1,#collectors.child do
                local collector = collectors.child[k]
                local x = collector.child[1].child[1].value
                local y = collector.child[1].child[2].value
                local filename = collector.child[8].value
                local sheetName = getSheetName(filename)
                local collector1 = collectorC.newCollector(imageSheet,sheets[sheetName],x,y);
                backgroundGroup:insert( collector1 )
                
            end
        end
        
        if(layer.child[i].properties["Name"] == "BACKGROUND")then
            local backgrounds = layer.child[i].child[1]
            for k=1,#backgrounds.child do
                local background = backgrounds.child[k]
                local x = background.child[1].child[1].value
                local y = background.child[1].child[2].value
                local rotation= math.deg(background.child[3].value)
                local scale = background.child[4].child[1].value
                local sizex = background.child[10].child[1].value * 2
                local sizey = background.child[10].child[2].value * 2
                local filename = background.child[8].value
                local sheetName = getSheetName(filename)
                local back1 = display.newImageRect(imageSheet,sheets[sheetName],sizex*scale,sizey*scale);
                back1.rotation = rotation
                back1.x = x
                back1.y = y
                
                backgroundGroup:insert( back1 )
                
            end
        end
        
    end
    
    return planetGroup,backgroundGroup
end

M.newLevel = newLevel

return M